import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
    Integer id;
    String name;
    Integer age;
    Boolean sex;
    Double score;

    public static void main(String[] args) {
        List<Student> students = new ArrayList<>();
        Collections.addAll(students,
            new Student(1,"xiao1hei",58,true,60.0),
            new Student(2,"xiao2hei",38,true,40.0),
            new Student(3,"xiao3hei",38,false,75.0),
            new Student(4,"xiao4hei",18,false,80.0),
            new Student(5,"xiao5hei",38,true,90.0),
            new Student(6,"xiao6hei",88,true,70.0),
            new Student(7,"xiao7hei",58,false,70.0),
            new Student(8,"xiao8hei",38,true,60.0)
        );

        //1 获取stream流
        //Stream<Student> stream = students.stream();
        //2 中间操作
        //stream.filter((stu) -> {
        //    return stu.getAge()<30;
        //});
        //3 终止操作
        //stream.forEach(System.out::println);
        // 简化成如下
        //students.stream().filter((stu)->stu.getAge()<30).forEach(System.out::println);

        long count = students.stream().count();
        System.out.println("count = " + count);
        //foreach
        students.stream().forEach((s)->System.out.println(s));
        //list,set
        List<Student> list = students.stream().collect(Collectors.toList());
        Set<Student> set = students.stream().collect(Collectors.toSet());
        //map中间操作
        Map<Boolean, List<Student>> map = students.stream().collect(Collectors.groupingBy(Student::getSex));
        List<Double> collect = students.stream().map((stu) -> stu.getScore()).collect(Collectors.toList());
        System.out.println(collect);
        //map+filter
        List<Double> collect1 = students.stream().filter((stu->stu.getAge()<30)).map((stu) -> stu.getScore()).collect(Collectors.toList());
        System.out.println(collect1);
        //reduce汇总
        Optional<Double> reduce = students.stream().map((stu) -> stu.getScore()).reduce((sum, s) -> {
            sum += s;
            return sum;
        });
        Double aDouble = reduce.get();
        System.out.println("最后汇总的分数："+aDouble);
        //peek窥探
        List<Student> peekCollect = students.stream().peek((stu) -> stu.setAge(stu.getAge() + 1)).collect(Collectors.toList());
        System.out.println(peekCollect);
        //skip跳过前几个
        System.out.println("skip:");
        students.stream().skip(3).forEach(System.out::println);
        //limit截取前几个
        System.out.println("limit:");
        students.stream().limit(5).forEach(System.out::println);
        //distinct去重
        students.stream().distinct().forEach(System.out::println);
        //sort排序
        System.out.println("sorted:");
        students.stream().sorted(Comparator.comparing(Student::getAge)).forEach(System.out::println);



//        List<Student> list1 = new ArrayList<>();
//        for(Student s: students){
//            if(s.getAge()>30){
//                list1.add(s);
//            }
//        }

//        List<Student> list2 = new ArrayList<>();
//        for (Student s : list1) {
//            if(s.getScore() < 80){
//                list2.add(s);
//            }
//        }
//
//        List<Student> list3 = new ArrayList<>();
//        List<Student> list4 = new ArrayList<>();
//        for (Student s : list2) {
//            if(s.getSex()){
//                list3.add(s);
//            }else{
//                list4.add(s);
//            }
//        }
//
//        List<String> list5 = new ArrayList<>();
//        List<String> list6 = new ArrayList<>();
//        for (Student s : list3) {
//            list5.add(s.getName());
//        }
//        for(Student s:list4){
//            list6.add(s.getName());
//        }
//
//        //提高效率：
//        List<String> list7 = new ArrayList<>();
//        List<String> list8 = new ArrayList<>();
//        for (Student s : students) {
//            if(s.getScore() < 80 && s.getAge()>30){
//                if(s.getSex() == true){
//                    list7.add(s.getName());
//                }else{
//                    list8.add(s.getName());
//                }
//            }
//        }
//
//        System.out.println(list5);
//        System.out.println("list7 = " + list7);
//        System.out.println(list6);
//        System.out.println("list8 = " + list8);
    }
}
